<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试页面</title>
    <script src="axios.js"></script>
    <script>

axios({
  method: 'post',
  url: '/excel',
  data: { ids: [1,2,3]},
  responseType: 'blob'
}).then(response => {
  exportFile(response)
}).catch((error) => {
    console.error(error);
})

function exportFile(result){
    const contentDisposition = result.headers['content-disposition'];
    const filename = decodeURI(contentDisposition.split('fileName=')[1] || contentDisposition.split('filename=')[1]);
    const blob = new Blob([result.data]);
    const url = window.URL.createObjectURL(blob);
    if (window.navigator.msSaveBlob) {
        try {
            window.navigator.msSaveBlob(blob, filename);
        }
        catch (e) {
            console.log(e);
        }
    }
    else {
 
        let link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', filename);
        document.body.appendChild(link);
        link.click();
    }
}

        // $.ajax({
        //         url: '/excel',
        //         type: 'post',
        //     }).done(function(data) {
        //         // ajax将返回数据转换为string，再利用该string创建Blob对象，下载的文件无法正确打开，数据可能已经被破坏
        //         var blob = new Blob([data],{"type":"text/html"});
        //         //对于Blob对象，我们可以创建出一个URL来访问它。使用URL对象的createObjectURL方法。
        //         var a = document.createElement('a');
        //         a.download = 'data.xlsx';
        //         a.href=window.URL.createObjectURL(blob);
        //         a.click()

        //     })
        

    </script>
</head>
<body>
    
</body>
</html>